home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / p_man / cat3 / SelfLoader.Z / SelfLoader
Encoding:
Text File  |  1998-10-28  |  12.2 KB  |  265 lines

  1.  
  2.  
  3.  
  4.      SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))   22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))     SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       SelfLoader - load functions only on demand
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.           package FOOBAR;
  13.           use SelfLoader;
  14.  
  15.           ... (initializing    code)
  16.  
  17.           __DATA__
  18.           sub {....
  19.  
  20.  
  21.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  22.       This module tells its    users that functions in    the FOOBAR
  23.       package are to be autoloaded from after the __DATA__ token.
  24.       See also the section on _A_u_t_o_l_o_a_d_i_n_g in the _p_e_r_l_s_u_b manpage.
  25.  
  26.       TTTThhhheeee ________DDDDAAAATTTTAAAA________ ttttooookkkkeeeennnn
  27.  
  28.       The __DATA__ token tells the perl compiler that the perl
  29.       code for compilation is finished. Everything after the
  30.       __DATA__ token is available for reading via the filehandle
  31.       FOOBAR::DATA,    where FOOBAR is    the name of the    current
  32.       package when the __DATA__ token is reached. This works just
  33.       the same as __END__ does in package 'main', but for other
  34.       modules data after __END__ is    not automatically retreivable
  35.       , whereas data after __DATA__    is.  The __DATA__ token    is not
  36.       recognized in    versions of perl prior to 5.001m.
  37.  
  38.       Note that it is possible to have __DATA__ tokens in the same
  39.       package in multiple files, and that the last __DATA__    token
  40.       in a given package that is encountered by the    compiler is
  41.       the one accessible by    the filehandle.    This also applies to
  42.       __END__ and main, i.e. if the    'main' program has an __END__,
  43.       but a    module 'require'd (_not_ 'use'd) by that program has a
  44.       'package main;' declaration followed by an '__DATA__', then
  45.       the DATA filehandle is set to    access the data    after the
  46.       __DATA__ in the module, _not_    the data after the __END__
  47.       token    in the 'main' program, since the compiler encounters
  48.       the 'require'd file later.
  49.  
  50.       SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr aaaauuuuttttoooollllooooaaaaddddiiiinnnngggg
  51.  
  52.       The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr works by the user placing the __DATA__    token
  53.       _a_f_t_e_r    perl code which    needs to be compiled and run at
  54.       'require' time, but _b_e_f_o_r_e subroutine    declarations that can
  55.       be loaded in later - usually because they may    never be
  56.       called.
  57.  
  58.       The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr will read from    the FOOBAR::DATA filehandle to
  59.       load in the data after __DATA__, and load in any subroutine
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))   22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))     SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))
  71.  
  72.  
  73.  
  74.       when it is called. The costs are the one-time    parsing    of the
  75.       data after __DATA__, and a load delay    for the    _first_    call
  76.       of any autoloaded function. The benefits (hopefully) are a
  77.       speeded up compilation phase,    with no    need to    load functions
  78.       which    are never used.
  79.  
  80.       The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr will stop reading from    __DATA__ if it
  81.       encounters the __END__ token - just as you would expect.  If
  82.       the __END__ token is present,    and is followed    by the token
  83.       DATA,    then the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr leaves the FOOBAR::DATA filehandle
  84.       open on the line after that token.
  85.  
  86.       The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr exports the AUTOLOAD subroutine to the
  87.       package using    the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr,    and this loads the called
  88.       subroutine when it is    first called.
  89.  
  90.       There    is no advantage    to putting subroutines which will
  91.       _always_ be called after the __DATA__    token.
  92.  
  93.       AAAAuuuuttttoooollllooooaaaaddddiiiinnnngggg aaaannnndddd ppppaaaacccckkkkaaaaggggeeee lllleeeexxxxiiiiccccaaaallllssss
  94.  
  95.       A 'my    $pack_lexical' statement makes the variable
  96.       $pack_lexical    local _only_ to    the file up to the __DATA__
  97.       token. Subroutines declared elsewhere    _cannot_ see these
  98.       types    of variables, just as if you declared subroutines in
  99.       the package but in another file, they    cannot see these
  100.       variables.
  101.  
  102.       So specifically, autoloaded functions    cannot see package
  103.       lexicals (this applies to both the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr    and the
  104.       Autoloader).    The vars pragma    provides an alternative    to
  105.       defining package-level globals that will be visible to
  106.       autoloaded routines. See the documentation on    vvvvaaaarrrrssss in    the
  107.       pragma section of the    _p_e_r_l_m_o_d    manpage.
  108.  
  109.       SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr aaaannnndddd AAAAuuuuttttooooLLLLooooaaaaddddeeeerrrr
  110.  
  111.       The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr can replace the AutoLoader - just change 'use
  112.       AutoLoader' to 'use SelfLoader' (though note that the
  113.       SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr exports the AUTOLOAD function - but if you    have
  114.       your own AUTOLOAD and    are using the AutoLoader too, you
  115.       probably know    what you're doing), and    the __END__ token to
  116.       __DATA__. You    will need perl version 5.001m or later to use
  117.       this (version    5.001 with all patches up to patch m).
  118.  
  119.       There    is no need to inherit from the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr.
  120.  
  121.       The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr works similarly to the    AutoLoader, but    picks
  122.       up the subs from after the __DATA__ instead of in the
  123.       'lib/auto' directory.     There is a maintainance gain in not
  124.       needing to run AutoSplit on the module at installation, and
  125.       a runtime gain in not    needing    to keep    opening    and closing
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))   22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))     SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))
  137.  
  138.  
  139.  
  140.       files    to load    subs. There is a runtime loss in needing to
  141.       parse    the code after the __DATA__. Details of    the AAAAuuuuttttooooLLLLooooaaaaddddeeeerrrr
  142.       and another view of these distinctions can be    found in that
  143.       module's documentation.
  144.  
  145.       ________DDDDAAAATTTTAAAA________,,,, ________EEEENNNNDDDD________,,,, aaaannnndddd tttthhhheeee FFFFOOOOOOOOBBBBAAAARRRR::::::::DDDDAAAATTTTAAAA ffffiiiilllleeeehhhhaaaannnnddddlllleeee....
  146.  
  147.       This section is only relevant    if you want to use the
  148.       FOOBAR::DATA together    with the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr.
  149.  
  150.       Data after the __DATA__ token    in a module is read using the
  151.       FOOBAR::DATA filehandle. __END__ can still be    used to    denote
  152.       the end of the __DATA__ section if followed by the token
  153.       DATA - this is supported by the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr. The FOOBAR::DATA
  154.       filehandle is    left open if an    __END__    followed by a DATA is
  155.       found, with the filehandle positioned    at the start of    the
  156.       line after the __END__ token.    If no __END__ token is
  157.       present, or an __END__ token with no DATA token on the same
  158.       line,    then the filehandle is closed.
  159.  
  160.       The SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr reads from wherever the current position of
  161.       the FOOBAR::DATA filehandle is, until    the EOF    or __END__.
  162.       This means that if you want to use that filehandle (and ONLY
  163.       if you want to), you should either
  164.  
  165.       1. Put all your subroutine declarations immediately after
  166.       the __DATA__ token and put your own data after those
  167.       declarations,    using the __END__ token    to mark    the end    of
  168.       subroutine declarations. You must also ensure    that the
  169.       SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr reads first by  calling 'SelfLoader-
  170.       >_l_o_a_d__s_t_u_b_s();', or by using a function which    is selfloaded;
  171.  
  172.       or
  173.  
  174.       2. You should    read the FOOBAR::DATA filehandle first,
  175.       leaving the handle open and positioned at the    first line of
  176.       subroutine declarations.
  177.  
  178.       You could conceivably    do both.
  179.  
  180.       CCCCllllaaaasssssssseeeessss aaaannnndddd iiiinnnnhhhheeeerrrriiiitttteeeedddd    mmmmeeeetttthhhhooooddddssss....
  181.  
  182.       For modules which are    not classes, this section is not
  183.       relevant.  This section is only relevant if you have methods
  184.       which    could be inherited.
  185.  
  186.       A subroutine stub (or    forward    declaration) looks like
  187.  
  188.         sub    stub;
  189.  
  190.       i.e. it is a subroutine declaration without the body of the
  191.       subroutine. For modules which    are not    classes, there is no
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))   22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))     SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr((((3333))))
  203.  
  204.  
  205.  
  206.       real need for    stubs as far as    autoloading is concerned.
  207.  
  208.       For modules which ARE    classes, and need to handle inherited
  209.       methods, stubs are needed to ensure that the method
  210.       inheritance mechanism    works properly.    You can    load the stubs
  211.       into the module at 'require' time, by    adding the statement
  212.       'SelfLoader->_l_o_a_d__s_t_u_b_s();' to the module to do this.
  213.  
  214.       The alternative is to    put the    stubs in before    the __DATA__
  215.       token    BEFORE releasing the module, and for this purpose the
  216.       Devel::SelfStubber module is available.  However this    does
  217.       require the extra step of ensuring that the stubs are    in the
  218.       module. If this is done I strongly recommend that this is
  219.       done BEFORE releasing    the module - it    should NOT be done at
  220.       install time in general.
  221.  
  222.      MMMMuuuullllttttiiiipppplllleeee ppppaaaacccckkkkaaaaggggeeeessss aaaannnndddd ffffuuuullllllllyyyy qqqquuuuaaaalllliiiiffffiiiieeeedddd ssssuuuubbbbrrrroooouuuuttttiiiinnnneeee nnnnaaaammmmeeeessss
  223.       Subroutines in multiple packages within the same file    are
  224.       supported - but you should note that this requires exporting
  225.       the SelfLoader::AUTOLOAD to every package which requires it.
  226.       This is done automatically by    the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr when it first
  227.       loads    the subs into the cache, but you should    really specify
  228.       it in    the initialization before the __DATA__ by putting a
  229.       'use SelfLoader' statement in    each package.
  230.  
  231.       Fully    qualified subroutine names are also supported. For
  232.       example,
  233.  
  234.          __DATA__
  235.          sub foo::bar {23}
  236.          package baz;
  237.          sub dob {32}
  238.  
  239.       will all be loaded correctly by the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr, and the
  240.       SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr will ensure that the packages 'foo' and 'baz'
  241.       correctly have the SSSSeeeellllffffLLLLooooaaaaddddeeeerrrr    AUTOLOAD method    when the data
  242.       after    __DATA__ is first parsed.
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.